package com.smcaiot.jftorg.north.local;

import com.smcaiot.core.exception.ApplicationDomainException;
import com.smcaiot.core.exception.ApplicationInfrastructureException;
import com.smcaiot.core.exception.DomainException;
import com.smcaiot.jftorg.domain.org.OrgId;
import com.smcaiot.jftorg.domain.user.User;
import com.smcaiot.jftorg.domain.user.UserService;
import com.smcaiot.jftorg.north.convert.UserReviewResponseConvert;
import com.smcaiot.jftorg.north.message.UserReviewResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: cz
 * @Date: 2023/9/27
 * @Description:
 */
@Service
public class UserAppService {

    @Autowired
    private UserService userService;

    public boolean isFujing(String role) {
        try {
            return new User(role).isFujing();
        } catch (DomainException ex) {
            throw new ApplicationDomainException(ex.getMessage(), ex);
        } catch (Exception ex) {
            throw new ApplicationInfrastructureException("Infrastructure Error", ex);
        }
    }

    public boolean isFJLeader(String role) {
        try {
            return new User(role).isFJLeader();
        } catch (DomainException ex) {
            throw new ApplicationDomainException(ex.getMessage(), ex);
        } catch (Exception ex) {
            throw new ApplicationInfrastructureException("Infrastructure Error", ex);
        }
    }

    public boolean isKSLeader(String role) {
        try {
            return new User(role).isKSLeader();
        } catch (DomainException ex) {
            throw new ApplicationDomainException(ex.getMessage(), ex);
        } catch (Exception ex) {
            throw new ApplicationInfrastructureException("Infrastructure Error", ex);
        }
    }

    public UserReviewResponse getUser(String userId) {
        try {
            User user = userService.getUser(OrgId.from(userId));
            return UserReviewResponseConvert.INSTANCE.from(user);
        } catch (DomainException ex) {
            throw new ApplicationDomainException(ex.getMessage(), ex);
        } catch (Exception ex) {
            throw new ApplicationInfrastructureException("Infrastructure Error", ex);
        }
    }

}
